package org.arestaurant;

import java.util.Map;

import javolution.util.FastMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class Food extends Activity {
    public static String FOOD_ID_SELECTED;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food);
        TextView foodNameTv = (TextView) findViewById(R.id.foodNameTv);
        TextView foodDescriptionTv = (TextView) findViewById(R.id.foodDescriptionTv);
        Map<String, String> parameters = FastMap.newInstance();
        parameters.put("productId", FOOD_ID_SELECTED);
        JSONObject product = FoodServices.getFood(ConnectionServices.connectHttp("/arestaurant/control/getFood", parameters));
        String productId = null;
        String internalName = null;
        String priceDetailText = null;
        String originalImageUrl = null;
        String productDescription = null;
        try {
            internalName = product.getString("internalName");
            productId = product.getString("productId");
            priceDetailText = product.getString("priceDetailText");
            originalImageUrl = product.getString("originalImageUrl");
            productDescription = product.getString("longDescription");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        foodNameTv.setText(productId + " - " + internalName + " - " + priceDetailText);
        foodDescriptionTv.setText(productDescription);
        Bitmap bm = ImageServices.getImageBitMap(originalImageUrl);
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(bm);
        imageView.setPadding(0, 200, 0, 0);
        addContentView(imageView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.food_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
        case R.id.refresh:
            intent = new Intent(this, Food.class);
            finish();
            startActivity(intent);
            return true;
        case R.id.order:
            intent = new Intent(this, Food.class);
            finish();
            startActivity(intent);
            return true;
        default:
            return true;
        }
    }
}
